/*
 * Copyright © 2019-2020  Whale Cloud, Inc. All Rights Reserved.
 *
 * Notice: Whale Cloud Inc copyrights this specification.
 * No part of this specification may be reproduced in any form or means,
 * without the prior written consent of Whale Cloud Inc.
 *
 */
package com.wteam.modules.system.service.impl;

import com.wteam.modules.system.service.CityService;
import com.wteam.modules.system.domain.City;
import com.wteam.modules.system.domain.dto.CityDTO;
import com.wteam.modules.system.domain.criteria.CityQueryCriteria;
import com.wteam.modules.system.domain.mapper.CityMapper;
import com.wteam.modules.system.repository.CityRepository;
import com.wteam.exception.BadRequestException;
import com.wteam.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.*;
import lombok.RequiredArgsConstructor;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 城市 业务实现层.
 *
 * @author herry_wong
 * @since 2020-12-28
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "city")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class CityServiceImpl implements CityService {

    private final CityRepository cityRepository;

    private final CityMapper cityMapper;

    private final RedisUtils redisUtils;

    @Override
    public Map<String,Object> queryAll(CityQueryCriteria criteria, Pageable pageable){
        Page<City> page = cityRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(cityMapper::toDto));
    }

    @Override
    public List<CityDTO> queryAll(CityQueryCriteria criteria){
        return cityMapper.toDto(cityRepository.findAll((root, criteriaQuery, criteriaBuilder) ->  QueryHelper.andPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Cacheable(key = "'id:' + #p0")
    public CityDTO findDTOById(Long id) {
        City city = cityRepository.findById(id).orElse(null);
        ValidUtil.notNull(city, City.ENTITY_NAME, "id", id);
        return cityMapper.toDto(city);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CityDTO create(City resources) {
        return cityMapper.toDto(cityRepository.save(resources));
    }

    @Override
    @CacheEvict(key = "'id:' + #p0.id")
    @Transactional(rollbackFor = Exception.class)
    public void update(City resources) {
        City city = cityRepository.findById(resources.getId()).orElse(null);
        ValidUtil.notNull(city, City.ENTITY_NAME, "id",resources.getId());

        city.copy(resources);
        cityRepository.save(city);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(Set<Long> ids) {
        redisUtils.delByKeys("city::id:", ids);
        cityRepository.logicDeleteInBatchById(ids);
    }

    @Override
    public void download(List<CityDTO> queryAll, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CityDTO city : queryAll) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("创建时间", city.getCreatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}